/**********************************************************************
 *<
	FILE..........: MapPath.cpp

	DESCRIPTION...: Bitmap Path Editor

	CREATED BY....: Christer Janson - Kinetix

	HISTORY.......: Created Thursday, October 16, 1997

 *>	Copyright (c) 1998, All Rights Reserved.
 **********************************************************************/
#include "stdafx.h"
#include "LaborMaxExporter.h"
#include <iassembly.h>
#include <iassemblymgr.h>
#include "AssetManagement/iassetmanager.h"
#include "IFileResolutionManager.h"

#include "3dsmaxport.h"

using namespace MaxSDK::AssetManagement;
RefCheck* RefCheck::ms_Singleton = NULL;
HINSTANCE		hInstance;
static TCHAR*	useFolder;

#pragma comment(lib,"comctl32.lib")
#pragma comment(lib,"bmm.lib") 
#pragma comment(lib,"core.lib") 
#pragma comment(lib,"geom.lib") 
#pragma comment(lib,"gfx.lib") 
#pragma comment(lib,"mesh.lib") 
#pragma comment(lib,"maxutil.lib") 
#pragma comment(lib,"maxscrpt.lib") 
#pragma comment(lib,"gup.lib") 
#pragma comment(lib,"paramblk2.lib")
#pragma comment(lib,"IGame.lib")

BOOL WINAPI DllMain(HINSTANCE hinstDLL,ULONG fdwReason,LPVOID lpvReserved)
{
   if( fdwReason == DLL_PROCESS_ATTACH )
   {
	   //AfxGetStaticModuleState();
	   AfxSetResourceHandle(hinstDLL);
	   AFX_MANAGE_STATE(NULL);
      hInstance = hinstDLL;				// Hang on to this DLL's instance handle.
      DisableThreadLibraryCalls(hInstance);
   }

	return (TRUE);
}


__declspec( dllexport ) const TCHAR* LibDescription()
{
	return GetString(IDS_LIBDESCRIPTION);
}

/// MUST CHANGE THIS NUMBER WHEN ADD NEW CLASS
__declspec( dllexport ) int LibNumberClasses()
{
	return 1;
}


__declspec( dllexport ) ClassDesc* LibClassDesc(int i)
{
	switch(i) {
		case 0: return GetRefCheckDesc();
		default: return 0;
	}
}

__declspec( dllexport ) ULONG LibVersion()
{
	return VERSION_3DSMAX;
}

__declspec( dllexport ) ULONG CanAutoDefer()
{
	return 1;
}



class RefCheckClassDesc:public ClassDesc {
	public:
	int 			IsPublic() {return 1;}
	void *			Create(BOOL loading = FALSE) {return &RefCheck::getSingleton();}
	const TCHAR *	ClassName() {return GetString(IDS_MODEL_EXPORTER);}
	SClass_ID		SuperClassID() {return UTILITY_CLASS_ID;}
	Class_ID		ClassID() {return REFCHECK_CLASS_ID;}
	const TCHAR* 	Category() {return _T("");}
};
   
static RefCheckClassDesc RefCheckDesc;
ClassDesc* GetRefCheckDesc() {return &RefCheckDesc;}

//class RefCheckFPInterface : public FPStaticInterface {
//protected:
//	DECLARE_DESCRIPTOR(RefCheckFPInterface)
//
//	enum {
//		kShowBitmapPathEditor = 1,
//	};
//
//	void DoDialog()
//	{
//		RefCheck::getSingleton().DoDialog(FALSE);
//	}
//
//	BEGIN_FUNCTION_MAP
//		VFN_0(kShowBitmapPathEditor, DoDialog)
//	END_FUNCTION_MAP
//
//	static RefCheckFPInterface sRefCheckFPInterface;
//};
//RefCheckFPInterface RefCheckFPInterface::sRefCheckFPInterface(
//	REFCHECK_FP_INTERFACE, _T("RefCheckInterface"), IDS_REFCHECK_INTERFACE, &RefCheckDesc, FP_STATIC_METHODS, 
//	kShowBitmapPathEditor, _T("ShowBitmapPathEditor"), 0, TYPE_VOID, 0, 0,
//	end);

TCHAR *GetString(int id)
{
	static TCHAR buf[256];

	if (hInstance)
		return LoadString(hInstance, id, buf, sizeof(buf)) ? buf : NULL;
	return NULL;
}

bool CommandDispatch(HWND hDlg,WPARAM wParam,LPARAM lParam);

static INT_PTR CALLBACK RefCheckDlgProc(
		HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
		case WM_INITDIALOG:
			RefCheck::getSingleton().Init(hWnd);
			break;

		case WM_DESTROY:
			RefCheck::getSingleton().Destroy(hWnd);
			break;

		case WM_COMMAND:{
			CommandDispatch(hWnd, wParam, lParam);
			}
			break;

		case WM_LBUTTONDOWN:
		case WM_LBUTTONUP:
		case WM_MOUSEMOVE:
			RefCheck::getSingleton().ip->RollupMouseMessage(hWnd,msg,wParam,lParam); 
			break;

		default:
			return FALSE;
	}
	return TRUE;
}	


//// RefCheck initlise
RefCheck::RefCheck() 
{
	iu		= NULL;
	ip		= NULL;	
	hPanel	= NULL;
	assert( !ms_Singleton );
	ms_Singleton = static_cast<RefCheck*>(this);
	//pDib	= NULL;
}

RefCheck::~RefCheck()
{
	assert( ms_Singleton ); 
	ms_Singleton = 0;  
}
// The dialog procedure for the modeless dialogbox
INT_PTR CALLBACK dlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

	//switch (msg) {
	//	case WM_INITDIALOG:
	//		{
	//			RECT rect;
	//			util = (BitmapPathEditorDialog*)lParam;
	//			util->hDialog = hWnd;
 //           DLSetWindowLongPtr(hWnd, lParam);
	//			CenterWindow(hWnd, util->mInterface.GetMAXHWnd());
	//			util->CheckDependencies();
	//			util->EnableEntry(hWnd, FALSE, 0);

	//			GetWindowRect(hWnd, &rect);
	//			util->SetMinDialogSize((rect.right - rect.left)*2/3, rect.bottom - rect.top);
	//			SendMessage(hWnd, WM_SETICON, ICON_SMALL, DLGetClassLongPtr<LPARAM>(util->mInterface.GetMAXHWnd(), GCLP_HICONSM));
	//		}
	//		break;
	//	case WM_COMMAND:
	//		switch (LOWORD(wParam)) {
	//			case IDCANCEL:
	//			case IDOK:
	//				//DestroyWindow(hWnd);
	//				EndDialog(hWnd, 1);
	//				break;
	//			case IDC_SETPATH:
	//				theRefCheck.mBitmapPathEditorDialog.Update();
	//				break;
	//			case IDC_STRIP_SELECTED:
	//				theRefCheck.mBitmapPathEditorDialog.StripSelected();
	//				break;
	//			case IDC_STRIP_ALL:
	//				theRefCheck.mBitmapPathEditorDialog.StripAll();
	//				break;
	//			case IDC_INFO:
	//				theRefCheck.mBitmapPathEditorDialog.ShowInfo();
	//				break;
	//			case IDC_COPYMAPS:
	//				theRefCheck.mBitmapPathEditorDialog.CopyMaps();
	//				break;
	//			case IDC_MISSING:
	//				theRefCheck.mBitmapPathEditorDialog.SelectMissing();
	//				break;
	//			case IDC_ACTUALPATH:
	//				theRefCheck.mBitmapPathEditorDialog.SetActualPath();
	//				break;
	//			case IDC_DEPLIST:
	//				if (HIWORD(wParam) == LBN_DBLCLK) {
	//					theRefCheck.mBitmapPathEditorDialog.ShowInfo();
	//				}
	//				else if (HIWORD(wParam) == LBN_SELCHANGE) {
	//					theRefCheck.mBitmapPathEditorDialog.DoSelection();
	//				}
	//				break;
	//			case IDC_BROWSE:
	//				theRefCheck.mBitmapPathEditorDialog.BrowseDirectory();
	//				break;
	//			case IDC_PATHEDIT:
	//				// If we get a setfocus message, disable accelerators
	//				if(HIWORD(wParam) == EN_SETFOCUS)
	//					DisableAccelerators();
	//				// Otherwise enable them again
	//				else if(HIWORD(wParam) == EN_KILLFOCUS) {
	//					EnableAccelerators();
	//				}
	//				break;
	//		}
	//		break;
	//	case WM_CLOSE:
	//		//DestroyWindow(hWnd);
	//		EndDialog(hWnd, 1);
	//		break;
	//	case WM_DESTROY:
	//		util->hDialog = NULL;
	//		break;
	//	case WM_WINDOWPOSCHANGING:
	//		{
	//			if(IsIconic(hWnd)) {
	//				return FALSE;
	//			}

	//			// prevent the window from stretching horizontally
	//			WINDOWPOS *wp = (WINDOWPOS*)lParam;
	//			if (wp->cx < util->GetMinDialogWidth()) {
	//				wp->cx = util->GetMinDialogWidth();
	//			}
	//			if (wp->cy < util->GetMinDialogHeight()) {
	//				wp->cy = util->GetMinDialogHeight();
	//			}

	//			break;
	//		}
	//	case WM_SIZE:
	//		util->ResizeWindow(LOWORD(lParam), HIWORD(lParam));
	//		break;
	//	default:
	//		return FALSE;
	//}
	return TRUE;
}

void RefCheck::BeginEditParams(Interface *ip,IUtil *iu) 
{
	this->iu = iu;
	this->ip = ip;

	

	hPanel = ip->AddRollupPage(
		hInstance,
		MAKEINTRESOURCE(IDD_PANEL),
		RefCheckDlgProc,
		GetString(IDS_PANELTITLE),
		0);
}
	
void RefCheck::EndEditParams(Interface *ip,IUtil *iu) 
{
	this->iu = NULL;
	this->ip = NULL;
	
	ip->DeleteRollupPage(hPanel);
	hPanel = NULL;
}

void RefCheck::Init(HWND hWnd)
{
	CheckDlgButton(hWnd, IDC_INCLUDE_MEDIT, BST_CHECKED);
}

void RefCheck::Destroy(HWND hWnd)
{
}

// My own version of ScreenToClient (taking Rect instead of Point)
void ScreenToClient(HWND hWnd, RECT* rect)
{
	POINT pt;
	pt.x = rect->left;
	pt.y = rect->top;
	ScreenToClient(hWnd, &pt);
	rect->left = pt.x;
	rect->top = pt.y;
	pt.x = rect->right;
	pt.y = rect->bottom;
	ScreenToClient(hWnd, &pt);
	rect->right = pt.x;
	rect->bottom = pt.y;
}


#define DXMATERIAL_DYNAMIC_UI Class_ID(0xef12512, 0x11351ed1)

bool IsDynamicDxMaterial(MtlBase * newMtl)
{

	DllDir * lookup = GetCOREInterface()->GetDllDirectory();
	ClassDirectory & dirLookup = lookup->ClassDir();

	ClassDesc * cd = dirLookup.FindClass(MATERIAL_CLASS_ID,newMtl->ClassID());
	if(cd && cd->SubClassID() == DXMATERIAL_DYNAMIC_UI)
		return true;
	else
		return false;
}

void RefCheck::DoDialog(BOOL ToCheckExternalMtlLibDependency)
{
	if (ToCheckExternalMtlLibDependency)
	{
	}
	else
	{
	}
}

bool RefCheck::GetIncludeMatLib()
{
	 return IsDlgButtonChecked(hPanel, IDC_USE_MATLIB) == BST_CHECKED;
}

bool RefCheck::GetIncludeMedit()
{
	 return IsDlgButtonChecked(hPanel, IDC_INCLUDE_MEDIT) == BST_CHECKED;
}
